var express = require('express');
var router = express.Router();
var { hisModel, cateModel, proModel } = require('../model/model')
// 耿义凯  分类后端


/* GET users listing. */
router.get('/list', async (req, res) => {
  let name = req.query.name
  // console.log(name);

  let names = new RegExp(name)
  let arr = [{}]
  if (name) {
    arr.push({ name: names })
  }
  let data = await proModel.find({ $and: arr }).populate()
  // console.log(data);
  res.send({ data: data });
});

router.get(`/xiang`, async (req, res) => {
  let id = req.query._id
  // console.log(id);

  let data = await proModel.find({ _id: id }).populate()
  // console.log(data);

  res.send({ data: data })
})

router.get(`/fen`, async (req, res) => {
  let data = await cateModel.find()
  res.send({ data: data })
})

router.post('/che', async (req, res) => {
  let _id = req.body._id
  let data = await proModel.find().populate()
  let shuju = ''
  data.forEach(i => {
    if (_id == i._id) {
      shuju = i
      // console.log(shuju);
      shuju.goods = 1
    }
  })
  await proModel.updateOne({ _id: _id }, shuju)
  // console.log(data);
})

router.post('/jia', async (req, res) => {
  let id = req.query._id
  console.log(id)
  let data = await proModel.find().populate()
  let shuju = ''
  data.forEach(i => {
    if (i._id == id) {
      shuju = i
      shuju.num += 1
      shuju.kucun -= 1
    }
  })
  await proModel.updateOne({ _id: id }, shuju)
  res.send('ok')
})

router.post('/jian', async (req, res) => {
  let id = req.query._id
  console.log(id)
  let data = await proModel.find().populate()
  let shuju = ''
  data.forEach(i => {
    if (i._id == id) {
      shuju = i
      shuju.num -= 1
      shuju.kucun += 1
    }
  })
  await proModel.updateOne({ _id: id }, shuju)
  res.send('ok')
})

router.post('/add', async (req, res) => {
  let name = req.body
  await hisModel.create(name)
  res.send('ok')
})

router.get('/his', async (req, res) => {
  let data = await hisModel.find()
  res.send({ data: data })
})



module.exports = router;